package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.domain.User;
import org.example.mapper.BookMapper;
import org.example.mapper.UserMapper;
import org.example.vo.Condition;
import org.example.vo.Result;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

public class mybatis_test {
    private SqlSession sqlSession=null;
    private  UserMapper mapper=null;
    private  BookMapper bookMapper=null;

    @Before//在任何测试方法之前会执行该方法
    public void before()throws IOException{
        //加载mybatis核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //创建Session工厂
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
        //3。创建session
         sqlSession = build.openSession(true);//开启自动提交
        //4.获取相应的mapper对象

         mapper = sqlSession.getMapper(UserMapper.class);
        bookMapper = sqlSession.getMapper(BookMapper.class);
    }
    @After//在任何测试方法之后会执行该方法
    public void after(){
        //关流
        sqlSession.close();

    }

    @Test
    public void query() throws Exception {
        mapper.findAll().forEach(System.out::println);
    }
    //
    @Test
    public void delete() throws Exception {
        mapper.delete(2);
    }
    @Test
    public void update() throws Exception {
        mapper.update(new User(5,"王麻子",33,"33333333"));
    }

    @Test
    public void save() throws Exception {
        mapper.save(new User(null,"王2麻子",33,"33333333"));
    }
    //计数
    @Test
    public void count() throws Exception{
        Long count = mapper.count();
        System.out.println("共查询到"+count+"条记录");
    }


    @Test
    public void findById() throws Exception{
        User user = mapper.findById(3);
        System.out.println(user);
    }

    /**
     * mybatis提供的方式传参
     * @throws Exception
     */
    @Test
    public void findByNameAndAge1() throws Exception{
        List<User> users = mapper.findByNameAndAge1("王麻子",33);
        System.out.println(users);
    }

    /**
     * 用对象传递参数
     * @throws Exception
     */
    @Test
    public void findByCondition() throws Exception{
        Condition condition = new Condition("王麻子", 33);
        List<User> users = mapper.findByCondition(condition);
        System.out.println(users);
    }

    /**
     * @param注解传参
     * @throws Exception
     */
    @Test
    public void findByNameAndAge() throws Exception{

        List<User> users = mapper.findByNameAndAge("王麻子",33);
        System.out.println(users);
    }

    /**
     * 只需要查出其中几个字段的时候，使用pojo类型接收返回值
     * @throws Exception
     */
    @Test
    public void findAll2() throws Exception{
        List<Result> result = mapper.findAll2();
        System.out.println(result);
    }

    /**
     * 使用map类型接收返回值
     * @throws Exception
     */
    @Test
    public void findUserById1() throws Exception{
        Map<String, Object> userById1 = mapper.findUserById1(3);
        System.out.println(userById1);
    }

    /**
     * 手动映射查询所有书籍
     * @throws Exception
     */
    @Test
    public void findAllBooks() throws Exception{
        bookMapper.findAllBooks().forEach(System.out::println);
    }
}
